<?php
require_once 'Kanedo/Db/Exception.php';
require_once 'Kanedo/Db/Interface.php';

class Kanedo_Db_Mysql implements Kanedo_Db_Interface 
{
	protected $host;
	protected $name;
	protected $pw;
	protected $database;
	
	protected $ressource;

	public function __construct($host, $name, $pw, $database)
	{
		$this->host = $host;
		$this->name = $name;
		$this->pw = $pw;
		$this->database = $database;
	}
	
	public function _connect()
	{
		$ressource = @mysql_connect($this->host, $this->name, $this->pw);
		if ($ressource)
		{
			if(!mysql_select_db($this->database, $ressource))
			{
				throw new Kanedo_Db_Exception("no_selected_db", mysql_errno());
			}
		}else {
			throw new Kanedo_Db_Exception("no_db_connection", mysql_errno());
		}
		$this->ressource = $ressource;
		return true;
	}
	
	public function executeQuery($sql)
	{
		return mysql_query($sql, $this->ressource);
	}
	
	public function escape($string)
	{
		return mysql_real_escape_string($string, $this->ressource);
	}
	
	public function getRessource()
	{
		return $this->ressource;
	}
	
	public function fetchData($sql, $type = 1)
	{
		$result = $this->executeQuery($sql);
		
	}
	public function fetchAll($result)
	{
		if(!$result){
			throw new Kanedo_Db_Exception('invalid_ressource {'.mysql_error($result).'}');
		}
		$return = array();
		$i = 0;
		while ($row = mysql_fetch_assoc($result))
		{
			$return[$i] = $row;
			$i++;
		}
		return $return;
	}
}
?>